<template>
	<div class="layout-error" v-if="showError">
		<img src="@/assets/404.png" alt="404" />
		<div>
			<el-button type="primary" @click="onToHome">{{ $t('back') }}</el-button>
			<!-- <el-button type="success" @click="onToHome">{{ $t('router.home') }}</el-button> -->
			<el-button type="success" @click="logout">退出</el-button>
		</div>
	</div>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import store from '@/store'
import { Storage } from '@/utils/storage'
import { encrypt, decrypt } from '@/utils/tool'
import { getUserPermission, postApi } from '@/api/api'

const router = useRouter()
const showError = ref(false)

const onBack = () => {
	router.back()
}
const onToHome = () => {
	router.replace('/')
}
const logout = () => {
	store.userStore.logoutAction().then(() => {
		if(Storage.getItem('user_system') == 2){
			router.push({ path: '/login' })
		}else{
			// 刷新页面
			location.reload()
		}
	})
}

onMounted(() => {
	if (!localStorage.getItem('token')) {
		showError.value = true;
	}
	// 如果token有效，则回到第一个页面去
	if (localStorage.getItem('user_system') == '2') {
		// 客户端
		// postApi('/api/client/permission/get_all_permission', { id: decrypt(Storage.getItem('router_key')) }, '')
		// 	.then((res: any) => {
		// 		console.log(res.data)
		// 		router.push({ path: '/PageProducts/Index' })
		// 	})
		// 	.catch(() => {})
	} else {
		// 管理端
		// getUserPermission({ id: decrypt(Storage.getItem('router_key')) })
		// 	.then((res: any) => {
		// 		const firstPage = res.data.findIndex((item: any) => item.route && item.route != '/')
		// 		if(firstPage != -1 && res.data.length > 0){
		// 			router.push({ path: '/' + res.data[0].route })
		// 		}else{
		// 			router.push({ path: '/' })
		// 		}
		// 	})
		// 	.catch(() => {})
	}
})
</script>

<style lang="scss" scoped>
.layout-error {
	img {
		width: 800px;
	}
	display: flex;
	flex-direction: column;
	align-items: center;
}
</style>
